---
title: 'Read me for replication materials'
subtitle: 'Kindt, Huber & Jacobs (2025) How populist citizens see the people and
their place in representative democracy, *West European Politics*'
author: Rosa Kindt, Robert A. Huber, Kristof Jacobs
output:
  rmdformats::downcute:
    use_bookdown: true
    self_contained: true
    thumbnails: false
    lightbox: true
    gallery: false
    highlight: monochrome
  editor_options:
  chunk_output_type: console
---

```{r globopt, include = F}
knitr::opts_chunk$set(warning = FALSE)
rm(list=ls())
```

This document guides through the replication for the following publication: Kindt, Huber and Jacobs (2025) 'How populist citizens see the people and their place in representative democracy', forthcoming in *West European Politics*. Thank you for your interest in our manuscript and the underlying data.

# How to proceed

All scripts required to replicate the results in `R` can be run from the file `master.R`. This file includes all analyses with the survey data necessary to replicate the paper. 

# Prepare Replication Data

After downloading our replication material from <https://doi.org/10.7910/DVN/5KSTRW>, please open the file `WEP_replication_materials.Rproj`. You need to have installed `R` (version 4.3x) and `RStudio` for this. After opening the `.Rproj` file in `RStudio`, please open the file `master.R` from the folder `r_scripts` within the project.

# Install/Load Packages

The following code installs all packages (if required). After that it loads the packages. The code then also uses the `here` package to set the working directory. Runtime of this code depends on the number of packages that you have already installed. However, even when you have no packages installed, it should only take a few minutes.

```{r pkgload, warning=FALSE, message=FALSE, results='hide'}
run_start_total <- Sys.time()
run_start <- Sys.time()

pkgs <- c("tidyverse",
          "haven",
          "clubSandwich",
          "ggh4x",
          "psych",
          "marginaleffects",
          "lm.beta")

# Function to check if packages are installed
# If not: package will be installed from CRAN and then loaded
# If: Package will be loaed

install_load <- function(packages){
  
  for (p in packages) {
    cat("Check package: '", p, "'...\n", sep = "")
    flush.console()
    
    if (p %in% rownames(installed.packages())) {
      
      cat("Package: '", p, "' is already installed...\n\n", sep = "")
      flush.console()
      
      library(p, character.only=TRUE)
      
    } else {
      
      cat("Package: '", p, "' is NOT installed! Will install now...\n\n", sep = "")
      install.packages(p)
      library(p,character.only = TRUE)
      
    }
  }
  cat("\nAll packages installed!\n\n")
}

# Apply function to all required packages

install_load(pkgs)

if ("cregg" %in% rownames(installed.packages())){
  cat("\ncregg is already installed")
} else{
  
  require(remotes)
  install_version("cregg", version = "0.4.0", repos = "http://cran.us.r-project.org")
}

# Set wd with here() package

here::i_am("WEP_replication_materials.Rproj")
```


```{r}
run_stop <- Sys.time()
run_time <- (run_stop - run_start)
run_time
```


# List of initial files

The files should cover the following:

```{r}
#List of files
list.files()
list.files(path = "./nonsharable_data/") #this data cannot be shared for legal reasons
list.files(path = "./final_data/") #this data contains the processed data
list.files(path = "./r_scripts/")
```

# Analysis

This script `master.R` runs all individual manuscripts and thereby replicates analyses shown in the paper and the appendix. The script `recode` loades the data from the folder `nonsharable_data` and processes it. The outcome is saved in folder `final_data`. `analysis.R` reproduces all analyses shown in the maintext of the paper and the Appendix. Its outputs are saved in the folders `figures` and `tables`. 

```{r, include=T, warning=FALSE, message=FALSE, results='hide', fig.show='hide'}
run_start <- Sys.time()

source("r_scripts/master.R", echo = T)

```

```{r}
run_stop <- Sys.time()
run_time <- (run_stop - run_start)
run_time
```

# List of created files

The replication files should create the following:

```{r}
#List of files
list.files("./figures/")
list.files("./tables/")
```

# Session Info

This notebook was run using the following setup:

```{r}
pander::pander(sessionInfo())

run_stop_total <- Sys.time()
run_time_total <- (run_stop_total - run_start_total)
run_time_total
```
